from django.contrib.auth.models import User as AuthUser
from django.core.exceptions import ObjectDoesNotExist
from blog import dao
from django.db import transaction
from common.models import Site, SiteTaxonomy, CategoryTaxonomy, UserProfile
def get_user(username):
    site = dao.get_local_site()
    try:
        user = AuthUser.objects.get(username=username, profile__site=site)
    except ObjectDoesNotExist:
        user = None
    return user

def new_user(username,password,email):
    site = dao.get_local_site()
    try:
        user = AuthUser.objects.get(username=username, profile__site=site)
        return None
    except ObjectDoesNotExist:
        with transaction.atomic():
            user = AuthUser.objects.create_user(username, email, first_name=username,password=password)
            user.save()
            user_profile = UserProfile(user=user, site=site)
            user_profile.save()
        return user
